package site.shihuan.helpdesk.service.manager;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.common.amqp.config.AmqpExchange;
import site.shihuan.helpdesk.common.amqp.config.AmqpQueue;
import site.shihuan.helpdesk.common.amqp.message.FinishServiceMsg;
import site.shihuan.helpdesk.common.amqp.message.StartServiceMsg;
import site.shihuan.helpdesk.common.amqp.util.AmqpMsgUtil;
import site.shihuan.helpdesk.common.util.IdWorker;
import site.shihuan.helpdesk.service.dao.SvcHiContactDao;
import site.shihuan.helpdesk.service.dao.SvcRuContactDao;
import site.shihuan.helpdesk.service.dao.SvcVisitorDao;
import site.shihuan.helpdesk.service.model.Constants;
import site.shihuan.helpdesk.service.model.po.SvcHiContactPO;
import site.shihuan.helpdesk.service.model.po.SvcRuContactPO;
import site.shihuan.helpdesk.service.model.po.SvcVisitorPO;
import site.shihuan.helpdesk.service.model.vo.VisitorVO;
import site.shihuan.helpdesk.service.service.ImService;

import java.util.Date;
import java.util.List;

/**
 * 客服联系人管理
 *
 * @author 周世焕
 * @Date 2021/3/2 10:38
 */
@Component
@Slf4j
public class ContactManager {

    @Autowired
    private SvcRuContactDao ruContactDao;

    @Autowired
    private SvcVisitorDao visitorDao;

    @Autowired
    private SvcHiContactDao hiContactDao;

    @Autowired
    private ImService imService;

    /**
     * 查询正在服务的客户
     * @param userId
     * @return
     */
    public List<VisitorVO> findContacts(String userId, String tenantId) {
        return ruContactDao.selectContacts(userId,tenantId);
    }

    /**
     * 结束客户服务联系
     * @param contactId
     */
    @Transactional(rollbackFor = Exception.class)
    public void finishContact(String contactId) {
        SvcRuContactPO svcRuContactPO = ruContactDao.selectByPrimaryKey(contactId);
        if(svcRuContactPO == null) {
            return;
        }
        /**
         * 更新Visitor状态
         */
        SvcVisitorPO visitorPO = new SvcVisitorPO();
        visitorPO.setVisitorId(svcRuContactPO.getVisitorId());
        visitorPO.setAccept(Constants.VISITOR_WAIT_STATE);
        visitorPO.setTenantId(svcRuContactPO.getTenantId());
        visitorDao.updateByPrimaryKeySelective(visitorPO);
        /**
         * 删除SvcRuContact运行时记录
         */
        ruContactDao.deleteByPrimaryKey(contactId);
        /**
         * 添加SvcHiContact历史记录
         */
        SvcHiContactPO hiContactPO = new SvcHiContactPO();
        hiContactPO.setContactId(svcRuContactPO.getContactId());
        hiContactPO.setClerkId(svcRuContactPO.getClerkId());
        hiContactPO.setTenantId(svcRuContactPO.getTenantId());
        hiContactPO.setVisitorId(svcRuContactPO.getVisitorId());
        hiContactPO.setStartTime(svcRuContactPO.getStartTime());
        hiContactPO.setEndTime(new Date(System.currentTimeMillis()));
        hiContactDao.insertSelective(hiContactPO);
        /**
         * 消息通知
         */
        imService.finishService(svcRuContactPO.getVisitorId());
    }

    /**
     * 客户接入受理
     * @param clerkId
     * @param visitorId
     */
    @Transactional(rollbackFor = Exception.class)
    public VisitorVO acceptVisitor(String clerkId, String visitorId, String tenantId) {
        /**
         * 更新Visitor状态
         */
        SvcVisitorPO visitorPO = new SvcVisitorPO();
        visitorPO.setVisitorId(visitorId);
        visitorPO.setAccept(clerkId);
        visitorPO.setTenantId(tenantId);
        visitorDao.updateByPrimaryKeySelective(visitorPO);
        /**
         * 添加到ChatRuntimeContact
         */
        SvcRuContactPO ruContactPO = new SvcRuContactPO();
        ruContactPO.setContactId(IdWorker.nextId());
        ruContactPO.setClerkId(clerkId);
        ruContactPO.setVisitorId(visitorId);
        ruContactPO.setTenantId(tenantId);
        ruContactPO.setStartTime(new Date(System.currentTimeMillis()));
        ruContactDao.insertSelective(ruContactPO);
        /**
         * 消息队列通知
         */
        imService.startService(ruContactPO.getTenantId(), ruContactPO.getVisitorId());
        return ruContactDao.selectContactVisitor(ruContactPO.getContactId());
    }
}
